package com.wlyy.bcwlw.sys.main.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wlyy.bcwlw.common.tree.BaseStaticTreeNode;
import com.wlyy.bcwlw.common.tree.StaticTree;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.SecurityFacade;
import com.wlyy.bcwlw.sys.application.entity.ApplicationDTO;
import com.wlyy.bcwlw.sys.menu.entity.MenuDTO;
import com.wlyy.bcwlw.sys.menu.entity.ModuleDTO;
import com.wlyy.bcwlw.sys.user.entity.UserProfile;


@Service
@Transactional
public class MainIndexService {
	
	
	
	public List<ApplicationDTO> getUserApps(){
		UserProfile curUser = SecurityFacade.getUserProfile();
		return curUser.getApps();
	}
	
	
	public TreeNode getOwnMenuTreeByAppId(String id){
		List<BaseStaticTreeNode> nodeList = new ArrayList<BaseStaticTreeNode>();
		ModuleDTO moduleDTO = new ModuleDTO();
		
		if(StringUtils.isNotEmpty(id)){
			moduleDTO.setAppId(id);
			nodeList.add(new BaseStaticTreeNode(id, "菜单", null, "app"));
		}
		UserProfile curUser = SecurityFacade.getUserProfile();
		List<ModuleDTO> sortModuleList = curUser.getModules();
		for(ModuleDTO dto:sortModuleList){
			BaseStaticTreeNode treeNode = new BaseStaticTreeNode(dto.getId(), dto.getName(), dto.getAppId(), "module");
			nodeList.add(treeNode);
		}
		List<MenuDTO> sortMenuList = curUser.getMenus();
		
		for (MenuDTO dto : sortMenuList) {
			BaseStaticTreeNode treeNode = new BaseStaticTreeNode(dto.getId(), dto.getName(), dto.getModuleId(), "menu",dto.getIcon(),dto.getUrl());
			nodeList.add(treeNode);
		}
		StaticTree tree = new StaticTree(nodeList.toArray(new BaseStaticTreeNode[0]),id);
		tree.render();
		return tree.getRootNode();
	}
}
